package com.zzh.greendaotest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zzh.sqllib.SQL;
import com.zzh.sqllib.bean.ActionBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.insert).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SQL.getInstance().getActionCount() > 0) {
                    Toast.makeText(MainActivity.this, "已经插入", Toast.LENGTH_SHORT).show();
                    return;
                }
                createData(new Date().getTime());
                Toast.makeText(MainActivity.this, "插入结束", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.select).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long time = new Date().getTime();
                List<ActionBean> actionBeans = SQL.getInstance().selectAllActions();
                long timenow = new Date().getTime() - time;
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < actionBeans.size(); i++) {
                    stringBuilder.append(actionBeans.get(i).toString());
                    stringBuilder.append("\n\n");
                }
                stringBuilder.append("time:" + timenow);
                stringBuilder.append("\n\n");
                stringBuilder.append("count:" + SQL.getInstance().getActionCount());
                ((TextView)findViewById(R.id.text)).setText(stringBuilder.toString());
            }
        });
        findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQL.getInstance().deleteAllActions();
            }
        });
        SQL.getInstance().init(this);
    }

    private List<ActionBean> testData (){
        List<ActionBean> actionBeans = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            ActionBean actionBean = new ActionBean();
            actionBean.setUserId(i + 1);
            actionBean.setFid(i + 1);
            actionBean.setName("zhang");
            actionBean.setDescription("fafasfasfasfadf");
            actionBean.setStartTime(new Date().getTime());
            actionBean.setRRULE("meiyou");
            actionBeans.add(actionBean);
        }
        return actionBeans;
    }

    private Random random ;

    public void createData (long time){
        if (random == null) random = new Random();
        int index = 0;
        int i = 1;
        while (index < 600) {
            ActionBean actionBean = createAction(i, i ,index * 864000000L + time);
            SQL.getInstance().inserte(actionBean);
            index += random.nextInt(10) + 1;
            i++;
        }
    }

    private ActionBean createAction (long userId, int fid, long time) {
        ActionBean actionBean = new ActionBean();

        actionBean.setUserId(userId);
        actionBean.setFid(fid);
        actionBean.setName("zhang");
        actionBean.setDescription("fafasfasfasfadf");
        actionBean.setStartTime(time);
        actionBean.setRRULE("meiyou");

        return actionBean;
    }

}
